<?php
namespace App\Http\Controllers\Client\Shop;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;

class ShopStoreController extends ClientController
{
    /**
     * 查询微信号对应的店铺
     */

    public function index()
    {
        $result = DB::table('shop')
            ->leftJoin('dict_area','shop.district','=','dict_area.id')
            ->leftJoin('explore_order as eo',function($join){
                $join->on('shop.shop_id','=','eo.shop_id')->where('eo.created_at','>=',date('Y-m-d').' 00:00:00'); // 今天的发单数量
            })
            ->where('shop.we_id',$this->we_id)
            ->groupBy('shop.shop_id')
            ->select('shop.shop_id','shop.shop_type','shop.shop_name','shop.contact_man','shop.contact_phone','shop.key_word','shop.head_img',
                'shop.province','shop.city','shop.district','shop.address','shop.longitude','shop.latitude','shop.platform','shop.verified','shop.verify_remark',
                'shop.link_url','shop.created_at','shop.updated_at','dict_area.short_name')
            ->selectRaw('COUNT(DISTINCT eo.eo_id) as order_count')
            ->orderByDesc('shop.updated_at')
            ->get()
            ->map(function($item){
                $item->type_name =  Data::$laifeiShopTypes[$item->shop_type];
                $item->lottery_permit = in_array($item->city,Data::$lotteryPermitCity);
                $item->head_img = AliOss::getUrl($item->head_img);
                $item->verified_name = Data::$laifeiShopVerified[$item->verified];
                return $item;
            })
            ->toArray();

        // 查询是否已经绑定了手机号
        $phone = DB::table('lottery_users')->where('we_id','=',$this->we_id)->value('phone');
        $verified_phone = $phone && true;

        // 店铺审核状态
        $verified_shop = DB::table('shop')
            ->where('we_id','=',$this->we_id)
            ->where('verified','=',Data::DATA_DB_LAIFEI_SHOP_VERIFIED_SUCCESS)
            ->count();
        $verified_shop = $verified_shop && true;

        return $this->setReturnJson(0,'success',[
            'info'=>$result,
            'verified_phone'=>$verified_phone,
            'verified_shop'=>$verified_shop,
        ]);




    }

}
